package cn.xz.mxxz.activity


import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.Map1Adapter
import cn.xz.mxxz.adapter.Map2Adapter
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.bean.*
import cn.xz.mxxz.databinding.ActivityMapBinding
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.net.OkHttpUtil
import cn.xz.mxxz.net.OkhttpResponseHandle
import cn.xz.mxxz.util.ToastUtil
import cn.xz.mxxz.util.gone
import cn.xz.mxxz.util.visible

/**
 * 企业地图*/
class MapActivity : BaseActivity<ActivityMapBinding>(R.layout.activity_map)  {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //当前界面请求标识
    private var mWsType = ""
    //一级列表适配器
    private var mList1Adapter: Map1Adapter? = null
    //企业地图(党建示范地)
    private val isMap = 13936

    override fun initData() {
        //获取当前设备信息
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            if (CommonLib.MY_LOCAL_IP.isNotEmpty() && mDevice != null){
                mDevice!!.ip = CommonLib.MY_LOCAL_IP
            }
            mDevice!!.ip = CommonLib.WEB_IP
            mDevice!!.port = CommonLib.WEB_PORT
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            //设置标题
            mDataBind.tvTitle.text = ControlDevice.getDeviceName(mTag)
            //区分请求表示
            initMine()
            //请求一级大标题
            getList1()
        }
    }

    //单独处理
    private fun initMine(){
        when(mTag){
            isMap ->{
                mWsType = "dq_build_web" //企业地图
                mDataBind.tvDisDialog.visible()
                mDataBind.tvStartG.gone()
                mDataBind.tvStopG.gone()
            }
            1->{
                mWsType = "dq_talent_web" //人才聚贤厅
                mDataBind.tvDisDialog.gone()
                mDataBind.tvStartG.visible()
                mDataBind.tvStopG.visible()
            }
        }
    }


    private fun getList1(){
        //文档上暂无
        /*val mUrl = "http://${mDevice!!.ip}:${mDevice!!.port}/business_dj /app/cockpit/getInfo"
        OkHttpUtil.get(this, mUrl,
            CockpitBean::class.java, object : OkhttpResponseHandle<CockpitBean?>() {
                override fun onSuccess(response: CockpitBean?) {
                    if (response != null && response.rows.isNotEmpty()){

                    }
                }
            })*/
        val mList: ArrayList<ListBean> = arrayListOf()
        if (mTag == isMap){
            mList.add(ListBean("","全部",true))
            mList.add(ListBean("1","企业带"))
            mList.add(ListBean("2","园区带"))
            mList.add(ListBean("3","党建阵地"))
        }else{
            mList.add(ListBean("0","全部",true))
            mList.add(ListBean("1","创客"))
            mList.add(ListBean("2","孵化器"))
            mList.add(ListBean("3","企业"))
        }


        mDataBind.rvLeft.layoutManager = LinearLayoutManager(this)
        mList1Adapter = Map1Adapter()
        mList1Adapter!!.setOnItemClickListener { adapter, view, position ->
            val mData =  adapter.data as ArrayList<ListBean>
            mData.forEach {
                it.select = false
                if (it.type == mData[position].type){
                    it.select = true
                }
            }
            adapter.notifyDataSetChanged()
            //给WEB发命令
            if (mTag == isMap){
                getList2(mData[position].type)
                val mType = mData[position].type
                val mBody = if (mType == ""){
                    0
                }else{
                    mType.toInt()
                }
                OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"typeChange",mBody,mWsType)
            }else{
                OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"typeChange",mData[position].type.toInt(),mWsType)
            }

        }
        mDataBind.rvLeft.adapter = mList1Adapter
        mList1Adapter?.setList(mList)

        if (mTag == isMap){
            //默认获取全部
            getList2("")
        }
    }

    private fun getList2(type: String){
        val mUrl = "http://${mDevice!!.ip}:${mDevice!!.port}/business_dj/app/enterprise/list?type=${type}"
        OkHttpUtil.get(this, mUrl,
            List2Data::class.java, object : OkhttpResponseHandle<List2Data?>() {
                override fun onSuccess(response: List2Data?) {
                     if (response != null && response.rows.isNotEmpty()){
                         setList2Data(response.rows)
                     }
                }
            })
    }

    private val mList2Adapter = Map2Adapter()
    private fun setList2Data(rows: ArrayList<ListBean2>){
        mDataBind.rvList2.layoutManager = LinearLayoutManager(this)
        mList2Adapter.setOnItemClickListener { adapter, view, position ->
            val mData =  adapter.data as ArrayList<ListBean2>
            mData.forEach {
                it.select = false
                if (it.id == mData[position].id){
                    it.select = true
                }
            }
            adapter.notifyDataSetChanged()
            OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"openWindow",mData[position].id.toInt(),mWsType)
        }
        mDataBind.rvList2.adapter = mList2Adapter
        mList2Adapter.setList(rows)
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //关闭弹窗
        mDataBind.tvDisDialog.setOnClickListener {
            val mData =  mList2Adapter.data as ArrayList<ListBean2>
            mData.forEach {
                it.select = false
            }
            mList2Adapter.notifyDataSetChanged()
            OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"openWindow",0,mWsType)
        }
        //开启滚动
        mDataBind.tvStartG.setOnClickListener {
            OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"playback",1,mWsType)
        }
        //暂停滚动
        mDataBind.tvStopG.setOnClickListener {
            OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"playback",0,mWsType)
        }
    }
}